﻿using System;
using System.Diagnostics;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace TFSBuildLight.Tasks
{
    public class BuildLight : TaskBase
    {
        public override bool Execute()
        {
            try
            {
                DateTime current = DateTime.Now;

                int starthours;
                int startminutes;
                int.TryParse(startTime.Split(':')[0], out starthours);
                int.TryParse(startTime.Split(':')[1], out startminutes);
                int stophours;
                int stopminutes;
                int.TryParse(endTime.Split(':')[0], out stophours);
                int.TryParse(endTime.Split(':')[1], out stopminutes);

                if (current.TimeOfDay < new TimeSpan(starthours, startminutes, 0) ||
                    current.TimeOfDay > new TimeSpan(stophours, stopminutes, 0))
                {
                    if (BuildEngine != null)
                    {
                        Log.LogMessage("Skipping DeviceController task. It is outside of start/stop times");
                        return true;
                    }
                }

                string turnoffArg = "";
                if (turnOffOtherDevices)
                {
                    turnoffArg = " TurnOffOtherDevices";
                }

                // I don't care what the house code and device code is anymore.
                // just send the command to WCF service
                string args = "\\\\" + TargetMachine + " " + DeviceControllerAppPathFileName + " -hc:" + houseCode + " -dc:" + deviceCode + " -c:" + command + turnoffArg;
                Process p = Process.Start(RemoteExecutePathFilename, args);
                p.WaitForExit();


                if (p.ExitCode != 0)
                {
                    string message = "DeviceController " + DeviceControllerAppPathFileName + " on " + targetMachine + " returned code:" + p.ExitCode;

                    if (ignoreExitCode)
                    {
                        Log.LogMessage(message);
                    }
                    else
                    {
                        Log.LogError(message);
                        return false;
                    }
                }

                if (BuildEngine != null)
                {
                    Log.LogMessage("DeviceController " + DeviceControllerAppPathFileName + " on " + targetMachine + " had been execute.");
                }
                return true;

            }
            catch (Exception ex)
            {
                if (BuildEngine != null)
                {
                    Log.LogError("Error executing DeviceController: DeviceController=" + DeviceControllerAppPathFileName + " TargetMachine=" +
                                 TargetMachine + " RemoteExecutePathFilename=" + RemoteExecutePathFilename + ":::" + ex);
                }
                return false;
            }
        }

        private string remoteExecutePathFilename;

        [Required]
        public string RemoteExecutePathFilename
        {
            get { return remoteExecutePathFilename; }
            set { remoteExecutePathFilename = value; }
        }

        private string targetMachine;

        [Required]
        public string TargetMachine
        {
            get { return targetMachine; }
            set { targetMachine = value; }
        }

        private string startTime;

        [Required]
        public string StartTime
        {
            get { return startTime; }
            set { startTime = value; }
        }

        private string endTime;

        [Required]
        public string EndTime
        {
            get { return endTime; }
            set { endTime = value; }
        }

        private string deviceControllerAppPathFileName;

        [Required]
        public string DeviceControllerAppPathFileName
        {
            get { return deviceControllerAppPathFileName; }
            set { deviceControllerAppPathFileName = value; }
        }

        private string houseCode;

        [Required]
        public string HouseCode
        {
            get { return houseCode; }
            set { houseCode = value; }
        }

        private int deviceCode;

        [Required]
        public int DeviceCode
        {
            get { return deviceCode; }
            set { deviceCode = value; }
        }

        private int command;

        [Required]
        public int Command
        {
            get { return command; }
            set { command = value; }

        }

        private bool turnOffOtherDevices;

        public bool TurnOffOtherDevices
        {
            get { return turnOffOtherDevices; }
            set { turnOffOtherDevices = value; }
        }
    }

}
